#!/bin/bash

set -xe

# 前端静态文件存放的位置
readonly MS_BASE=/opt/metersphere
readonly FRONTEND_OEM=${MS_BASE}/oem
readonly TAG=v2.10.20-lts
readonly DOCKER_IMG0=registry.cn-qingdao.aliyuncs.com/metersphere/api-test:${TAG}
readonly DOCKER_IMG1=registry.cn-qingdao.aliyuncs.com/metersphere/gateway:${TAG}
readonly DOCKER_IMG2=registry.cn-qingdao.aliyuncs.com/metersphere/performance-test:${TAG}
readonly DOCKER_IMG3=registry.cn-qingdao.aliyuncs.com/metersphere/project-management:${TAG}
readonly DOCKER_IMG4=registry.cn-qingdao.aliyuncs.com/metersphere/report-stat:${TAG}
readonly DOCKER_IMG5=registry.cn-qingdao.aliyuncs.com/metersphere/system-setting:${TAG}
readonly DOCKER_IMG6=registry.cn-qingdao.aliyuncs.com/metersphere/test-track:${TAG}
readonly DOCKER_IMG7=registry.cn-qingdao.aliyuncs.com/metersphere/workstation:${TAG}
readonly TARGET_FRONTEND=/opt/metersphere/frotnend


#!/bin/bash

# 定义路径列表
paths=(
  ${FRONTEND_OEM}/api-test-frontend
#  ${FRONTEND_OEM}/gateway-frontend
  ${FRONTEND_OEM}/performance-test-frontend
  ${FRONTEND_OEM}/project-management-frontend
  ${FRONTEND_OEM}/report-stat-frontend
#    ${FRONTEND_OEM}/system-setting-frontend
  ${FRONTEND_OEM}/test-track-frontend
  ${FRONTEND_OEM}/workstation-frontend
)

# 遍历路径列表
for path in "${paths[@]}"; do
  if [ -f "$path" ]; then
    echo "文件 $path  已存在，退出执行。"
    exit 1
  elif [ -d "$path" ]; then
    echo "目录 $path 已存在，退出执行。"
    exit 1
  fi
done

#=============================================

mkdir -pv ${FRONTEND_OEM}/workstation-frontend
docker run -d -v ${FRONTEND_OEM}/workstation-frontend:/frontend-oem ${DOCKER_IMG7} cp -rf /app/static/ /frontend-oem

# 更新 compose 文件, 添加 volume
pushd ${MS_BASE}
sed -i -e '/ms-data/a \      - \/opt\/metersphere\/oem\/workstation-frontend\/static\/:\/app\/static' \
  docker-compose-workstation.yml
popd
# 重启 gateway 容器, 以便应用新的 volume 列表
pushd ${MS_BASE}
docker-compose -f docker-compose-base.yml -f docker-compose-system-setting.yml -f docker-compose-workstation.yml down
docker-compose -f docker-compose-base.yml -f docker-compose-system-setting.yml -f docker-compose-workstation.yml up -d
popd

#=============================================

mkdir -pv ${FRONTEND_OEM}/performance-test-frontend
docker run -d -v ${FRONTEND_OEM}/performance-test-frontend:/frontend-oem ${DOCKER_IMG2} cp -rf /app/static/ /frontend-oem

# 更新 compose 文件, 添加 volume
pushd ${MS_BASE}
sed -i -e '/ms-data/a \      - \/opt\/metersphere\/oem\/performance-test-frontend\/static\/:\/app\/static' \
  docker-compose-performance-test.yml
popd
# 重启 gateway 容器, 以便应用新的 volume 列表
pushd ${MS_BASE}
docker-compose -f docker-compose-base.yml -f docker-compose-system-setting.yml -f docker-compose-performance-test.yml down
docker-compose -f docker-compose-base.yml -f docker-compose-system-setting.yml -f docker-compose-performance-test.yml up -d
popd

#=============================================

mkdir -pv ${FRONTEND_OEM}/project-management-frontend
docker run -d -v ${FRONTEND_OEM}/project-management-frontend:/frontend-oem ${DOCKER_IMG3} cp -rf /app/static/ /frontend-oem

# 更新 compose 文件, 添加 volume
pushd ${MS_BASE}
sed -i -e '/ms-data/a \      - \/opt\/metersphere\/oem\/project-management-frontend\/static\/:\/app\/static' \
  docker-compose-project-management.yml
popd
# 重启 gateway 容器, 以便应用新的 volume 列表
pushd ${MS_BASE}
docker-compose -f docker-compose-base.yml -f docker-compose-system-setting.yml -f docker-compose-project-management.yml down
docker-compose -f docker-compose-base.yml -f docker-compose-system-setting.yml -f docker-compose-project-management.yml up -d
popd

#=============================================

mkdir -pv ${FRONTEND_OEM}/report-stat-frontend
docker run -d -v ${FRONTEND_OEM}/report-stat-frontend:/frontend-oem ${DOCKER_IMG4} cp -rf /app/static/ /frontend-oem

# 更新 compose 文件, 添加 volume
pushd ${MS_BASE}
sed -i -e '/ms-data/a \      - \/opt\/metersphere\/oem\/report-stat-frontend\/static\/:\/app\/static' \
  docker-compose-report-stat.yml
popd
# 重启 gateway 容器, 以便应用新的 volume 列表
pushd ${MS_BASE}
docker-compose -f docker-compose-base.yml -f docker-compose-system-setting.yml -f docker-compose-report-stat.yml down
docker-compose -f docker-compose-base.yml -f docker-compose-system-setting.yml -f docker-compose-report-stat.yml up -d
popd

#=============================================

mkdir -pv ${FRONTEND_OEM}/api-test-frontend
docker run -d -v ${FRONTEND_OEM}/api-test-frontend:/frontend-oem ${DOCKER_IMG0} cp -rf /app/static/ /frontend-oem

# 更新 compose 文件, 添加 volume
pushd ${MS_BASE}
sed -i -e '/ms-data/a \      - \/opt\/metersphere\/oem\/api-test-frontend\/static\/:\/app\/static' \
  docker-compose-api-test.yml
popd
# 重启 gateway 容器, 以便应用新的 volume 列表
pushd ${MS_BASE}
docker-compose -f docker-compose-base.yml -f docker-compose-system-setting.yml -f docker-compose-api-test.yml down
docker-compose -f docker-compose-base.yml -f docker-compose-system-setting.yml -f docker-compose-api-test.yml up -d
popd

#=============================================

mkdir -pv ${FRONTEND_OEM}/test-track-frontend
docker run -d -v ${FRONTEND_OEM}/test-track-frontend:/frontend-oem ${DOCKER_IMG6} cp -rf /app/static/ /frontend-oem

# 更新 compose 文件, 添加 volume
pushd ${MS_BASE}
sed -i -e '/ms-data/a \      - \/opt\/metersphere\/oem\/test-track-frontend\/static\/:\/app\/static' \
  docker-compose-test-track.yml
popd
# 重启 gateway 容器, 以便应用新的 volume 列表
pushd ${MS_BASE}
docker-compose -f docker-compose-base.yml -f docker-compose-system-setting.yml -f docker-compose-test-track.yml down
docker-compose -f docker-compose-base.yml -f docker-compose-system-setting.yml -f docker-compose-test-track.yml up -d
popd


# 拷贝资源文件
# cp -r ./static_tools/images/* ${GATEWAY_OEM}/static/assets
# cp -r ./static_tools/js/app.8144b0b2.js ${GATEWAY_OEM}/static/js
# cp -r ./static_tools/index.html ${GATEWAY_OEM}/static/


# 取消 docker-compose-gateway.yml 中的 volume
# pushd ${MS_BASE}
# sed -i -e '/gateway-oem/d' docker-compose-gateway.yml
# popd
# #重启 gateway 容器, 以便应用新的 volume 列表
# pushd ${MS_BASE}
# docker-compose -f docker-compose-base.yml -f docker-compose-gateway.yml down
# docker-compose -f docker-compose-base.yml -f docker-compose-gateway.yml up -d
# popd
